<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class IndexSellingWater extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_selling_water';

    protected $append = [
        'extract_money_text'
    ];

    /**
     * @param $value
     * @param $data
     * @return float|int
     * 佣金
     *
     */
    public function getExtractMoneyTextAttr($value,$data){

        if($data['selling_type']==0){

            $money  = $data['extract'] * $data['price'] / 100;

        }else{

            $money  = $data['extract_cash'];
        }

        return round($money,2);

    }

    /**
     * @param $value
     * @return array
     * 转图片
     */
    public function getImgAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:[];
    }


    /**
     * @param $dis
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取流水信息
     */
    public function waterInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加流水
     */
    public function waterAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $data
     * @return int|string
     * 编辑流水
     */
    public function waterUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查找记录
     */
    public function waterSelect($dis){
        $data = $this->where($dis)->select()->toArray();
        return $data;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 佣金记录列表
     */
    public function waterList($dis){
        $data = $this->where($dis)->order('id desc')->paginate(10)->toArray();
        $data['data'] = lbHandelRadarDate($data['data']);
        $data['data'] = transImagesOne($data['data'],['img']);
        return $data;
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 佣金记录列表
     */
    public function waterListV2($dis,$where,$page){

        $a = Db::name('longbing_card_commission_water')->field(['id','null as title','target_id','null as img','null as price','create_time','cash as extract_cash','status as waiting','num as buy_number','type','order_id','selling_type','null as extract'])->where($where)->order('create_time desc,id desc')->buildSql();

        $page = intval($page);

        $data = $this->where($dis)->field(['id','title','null as target_id','img','price','create_time','extract_cash','waiting','buy_number','type','order_id','selling_type','extract'])->unionAll([$a])->order('create_time desc,id desc')->paginate(10,$page)->toArray();

        $data['data'] = lbHandelRadarDate($data['data']);

        $data['data'] = transImagesOne($data['data'],['img']);

        if(!empty( $data['data'])){

            foreach ( $data['data'] as $k=>&$value){
                //新的分销
                if(!empty($value['target_id'])){
                    //满减返佣
                    if($value['type']==0){
                        //标题
                        $value['title'] = Db::name('longbing_card_reduction_active')->where(['id'=>$value['target_id']])->value('title');
                        //
                        $value['price'] = Db::name('longbing_card_reduction_pay_record')->where(['id'=>$value['id']])->value('pay_price');

                    }else{
                        //储值
                        $value['title'] = Db::name('longbing_card_member_stored_order')->where(['id'=>$value['order_id']])->value('title');

                        $value['price'] = Db::name('longbing_card_member_stored_order')->where(['id'=>$value['order_id']])->value('pay_price');

                    }
                    //
                    $value['extract_money_text'] = $value['extract_cash'];
                }

            }

        }
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 10:52
     * @param $dis
     * @return void
     * descption:退还流水
     */
    public function refundWater($dis){
        $water = $this->where($dis)->select()->toArray();
        if(!empty($water)){
            foreach ($water as $value){
                $this->waterUpdate(['id'=>$value['id']],['status'=>0]);

                $money  = $value[ 'price' ] * $value[ 'extract' ]/100;

                if($value['selling_type']==1){

                    $money += $value['extract_cash'];
                }
                $money  = sprintf( "%.2f", $money );
                $profit = Db::name('longbing_card_selling_profit')->where(['user_id' => $value['user_id']])->find();
                if (!empty($profit)&&$profit[ 'waiting' ]>0){
                    $waiting = $profit[ 'waiting' ] - $money;
                    $waiting = floatval($waiting)<0?0:floatval($waiting);
                    Db::name('longbing_card_selling_profit')->where(['id' => $profit['id']])->update(['waiting' => $waiting]);
                }
            }
        }
        return true;
    }







}